//
//  XOFlashListHeaderCollectionReusableView.m
//  didigo
//
//  Created by 海丰丛 on 2019/12/4.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOFlashListHeaderCollectionReusableView.h"
#import "EJSFlashSaleModel.h"

@interface XOFlashListHeaderCollectionReusableView ()

@end

@implementation XOFlashListHeaderCollectionReusableView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flashSaleTimerCountDown) name:@"flashaTimerCountDownNotif" object:nil];
}

- (void)setSaleModel:(EJSFlashSaleModel *)saleModel {
    _saleModel = saleModel;
    
    [self flashSaleTimerCountDown];
    
}

- (void)flashSaleTimerCountDown {
    
    if(![self flashSaleUnderway]){
        self.timeCover.hidden = YES;
        self.statusLabel.text = [NSString stringWithFormat:@"下一场抢购 %@ 开始", self.saleModel.startTime];
        return;
    }else{
        self.statusLabel.text = @"抢购中";
    }

    
    NSDate *flashSaleEndDate = [self convertStringToDate:self.saleModel.endTime];
    NSDate *localDate = [self localDate];
    NSTimeInterval start = (NSInteger)[localDate timeIntervalSince1970]*1;
    NSTimeInterval end = (NSInteger)[flashSaleEndDate timeIntervalSince1970]*1;

    NSTimeInterval timeStamp = end - start;
    if(timeStamp > 0){
        int second = (int)timeStamp%60;//秒
        int minute = (int)timeStamp/60%60;
        int hour = (int)timeStamp/3600;
        NSString *hourString  = [NSString stringWithFormat:@"%02d",hour];
        NSString *minuteString  = [NSString stringWithFormat:@"%02d",minute];
        NSString *secondString  = [NSString stringWithFormat:@"%02d",second];

        self.timeCover.hidden = NO;
        self.hTimeLabel.text = hourString;
        self.mTimeLabel.text = minuteString;
        self.sTimeLabel.text = secondString;
        
    }else{
        self.saleModel = _saleModel;
    }
    
}

- (BOOL)flashSaleUnderway{
    NSDate *flashStartDate = [self convertStringToDate:self.saleModel.startTime];
    NSDate *flashEndDate = [self convertStringToDate:self.saleModel.endTime];
    NSComparisonResult result = [[self localDate] compare:flashStartDate];
    NSComparisonResult result2 = [[self localDate] compare:flashEndDate];
    if((result == NSOrderedDescending || result == NSOrderedSame) && (result2 == NSOrderedAscending || result2 == NSOrderedSame)){
        return YES;
    }
    return NO;
}

- (NSDate *)localDate{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    return  [date  dateByAddingTimeInterval: interval];
}

- (NSDate *)convertStringToDate:(NSString *)string{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];//时间管理
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    NSDate *date = [formatter dateFromString:string];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    return  [date dateByAddingTimeInterval:interval];
}


@end
